package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.CustomException;
import com.itheima.common.R;
import com.itheima.entity.Category;
import com.itheima.entity.Dish;
import com.itheima.entity.Setmeal;
import com.itheima.mapper.CategoryMapper;
import com.itheima.service.CategoryService;
import com.itheima.service.DishService;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Auther: guozy
 * @Date: 2022/7/30 - 07 - 30 - 19:02
 * @Description: com.itheima.service.impl
 * @version: 1.0
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;


    @Override
    public void categoryRemove(Long id) {
        LambdaQueryWrapper<Dish> dlqw = new LambdaQueryWrapper<>();
        //添加查询条件分局id 进行查询
        dlqw.eq(Dish::getCategoryId,id);
        int count = dishService.count(dlqw);
        //判断是否联系菜品
        if (count>0){
            //抛出异常,说明跟菜品有关联,不能删除
            throw new CustomException("菜品有关联,无法删除");
        }
        LambdaQueryWrapper<Setmeal> slqw = new LambdaQueryWrapper<>();
        //添加查询条件
        slqw.eq(Setmeal::getCategoryId,id);
        int num = setmealService.count(slqw);
        //判断是否联系套餐
        if (num>0){
            //抛出异常,说明跟套餐有联系,不能删除
            throw new CustomException("套餐有关联,无法删除");
        }
        //调用父类方法删除id
        super.removeById(id);
    }
}















